package com.excel.validators.excel;

import java.io.File;
import java.io.InputStream;
import java.util.Collection;
import java.util.Date;

import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFRow;

import com.excel.validators.core.decorative.BasicClass;
import com.excel.validators.core.decorative.CompositeValue;
import com.excel.validators.core.decorative.FieldElement;
import com.excel.validators.core.decorative.FieldValue;
import com.excel.validators.core.extension.TypeConverter;

/**
 * @author wangxiaoliang10
 * 
 * 继承write/read excel handler
 *
 */
@SuppressWarnings("rawtypes")
public class ExcelInnerHandler extends ModelHandler {
		
		public ExcelInnerHandler(){
				super();
		}
		/**
		 * construct with namespace
		 * @param namespace
		 * @param excelFile
		 */
		public ExcelInnerHandler(File excelFile, int worksheet) {
				super(excelFile, worksheet);
		}
	
		public ExcelInnerHandler(InputStream inStream) {
				super(inStream);
		}

		@Override
		protected CompositeValue buildComposite(XSSFRow xssrow) {
				
				Collection<FieldElement> fieldElements = get().getFieldEntry().values();
				
				if (!fieldElements.isEmpty()) {
						CompositeValue composite = new CompositeValue(fieldElements.size());
						for (FieldElement field : fieldElements) {
								// TODO get cell content
								XSSFCell cell = xssrow.getCell(field.getFieldIndex());
								try {
										//	 TODO create audit object
										FieldValue value = field.newInstance(screen(cell, field));
										composite.addChild(value);
								} catch (Exception e) {
										logger.error("field type inject faild " + field.getFieldName(),e);
								}
								removeCellStyle(cell);
						}
						return composite;
				}
				
				return null;
		}

		/**
		 * 删除单元格错误样式及注释
		 * @param cell
		 */
		private void removeCellStyle(XSSFCell cell) {
				if (cell != null) {
						XSSFComment comment = cell.getCellComment();
						if (comment != null) {
								String author = comment.getAuthor();
								if (equals(author, StyleClass.author)) {
										cell.setCellComment(null);							// clear comment
								}
						}
						XSSFCellStyle errorstyle = cell.getCellStyle();
						if (errorstyle != null) {
								short error = IndexedColors.RED.index;
								short backgroundColor = errorstyle.getFillBackgroundColor();
								short foregroundColor = errorstyle.getFillForegroundColor();
								if (error == backgroundColor
										|| error == foregroundColor) {
										errorstyle.setFillBackgroundColor(IndexedColors.SEA_GREEN.index);		// clear error style
										errorstyle.setFillForegroundColor(IndexedColors.SEA_GREEN.index);
								}
						}
				}
		}
		/**
		 * check is not date type
		 * @param model
		 * @param cell
		 * @param model 
		 * @return
		 */
		private String screen(XSSFCell cell, FieldElement field) {
				if (cell != null) {
						int basic = field.getBasicClass();
						if (basic == BasicClass.DATE) {
								// check date type
								if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
										return getDate(cell,field.getPattern());
								}
						}
						String value = cell.toString().trim();
						return value.length() == 0 ? null : value ;
				}
				return null;
		}
	
		private String getDate(XSSFCell cell, String pattern) {
				try {
						Date date = cell.getDateCellValue();
						return TypeConverter.formatDate(date,pattern);
				} catch (Exception e) {
						// TODO: handle exception
				}
				return cell.toString();
		}
}
